package com.gxuwz.attendance.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxuwz.attendance.entity.Record;
import com.gxuwz.attendance.service.ClassService;
import com.gxuwz.attendance.service.RecordService;
import com.gxuwz.attendance.util.JsonUtil;
import com.gxuwz.attendance.vo.Page;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class RecordAction extends BaseAction implements ModelDriven<Record>,Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String TEACHER_RECORD = "/page/attendance/attendance_record.jsp";
	private Record record;
	@Autowired
	private RecordService service;
	@Autowired
	private ClassService classService;
	private String rows;
	private String page;
	private String type;
	private String num = null;
	private String data;
	private String ids;
	private Map<String, Object> map = new HashMap<String, Object>();
	
	
	/**
	 * 更新考勤记录
	 */
	public void  update() {
		boolean flag = service.update(record);
		if(flag == false) {
			map.put("type", "error");
			map.put("msg", "修改失败");
		}else {
			map.put("type", "success");
			map.put("msg", "修改成功");
		}
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	/**
	 * 查询考勤记录
	 */
	public void recordList() {
		List<Record> recordList = service.recordList(record, new Page(Integer.parseInt(page), Integer.parseInt(rows)));
		
		map.put("rows", recordList);
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		map.put("total", session.getAttribute("recordSize"));
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
		
	}
	/**
	 * 跳转到考勤记录页面
	 * @return
	 */
	public String toRecord() {
		classService.findByName(null);
		forwardView = TEACHER_RECORD;
		
		return SUCCESS;
	}
	
	
	/**
	 * 批量删除考勤记录
	 */
	public void delete() {
		System.out.println("ids" + ids);
		boolean flag = true;
		try {
			flag = service.delete(ids);
		}catch (Exception e) {
			map.put("type", "error");
			map.put("msg", "删除失败");
			e.printStackTrace();
		}
		map.put("type", "success");
		map.put("msg", "删除成功");
		if(flag == false) {
			map.put("type", "error");
			map.put("msg", "删除失败");
		}
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	@Override
	public void prepare() throws Exception {
		if(record == null) {
			record = new Record();
		}

	}

	@Override
	public Record getModel() {
		return record;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getType() {
		return type;
	}



	public void setType(String type) {
		this.type = type;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	
}
